package com.lds.erp.weixin;


import com.lds.erp.configuration.ConfigProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;


@RestController
@RequestMapping(value = "/api/WeixinQR/")
public class WeixinQRController {

    @Autowired
    private ConfigProperty configProperty;

    private String output = "";
    private String outputstr = "/output/";
    private String imageType = "jpg";
    @GetMapping(value = "/wechat/getWechatqr")
    public void getWechatqr(String inviteCode, HttpServletResponse response) {
        output = "/home/fei"  + outputstr;
        InputStream inputStream = null;
        if (inviteCode != null)
        {
            try {
                File f = new File(output  + inviteCode +  "."  + imageType);
                inputStream = new FileInputStream(f);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        response.setContentType("image/png");
        try {
            BufferedImage bi = ImageIO.read(inputStream);
            ImageIO.write(bi, "PNG", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
